package com.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.config.GuliException;
import com.edu.entity.UcenterMember;
import com.edu.entity.vo.RegisterVo;
import com.edu.service.UcenterMemberService;
import com.edu.utils.JwtUtils;
import com.edu.utils.MD5;
import com.edu.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author mxs
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/educenter")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;
    @Autowired
    RedisTemplate<String, String> redisTemplate;

    /***
     * 登录
     * @param member
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember member) {
        //调用service实现登录校验
        //获取token值
        String token = ucenterMemberService.login(member);
        return R.success().data("token", token);
    }

    /**
     * 注册
     *
     * @param registerVo
     * @return
     */



    /**
     * 根据token获取用户信息
     *
     * @return
     */
    @GetMapping("/getMemberInfo/token")
    public R getMemberInfo(HttpServletRequest request) {
        //根据token获取用户的id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.success().data("member", member);
    }

    @PostMapping("/register")
    public R register(@RequestBody(required = true) RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        //判断传过来的代码是否为空
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
            return R.error().message("部分信息为空");
        }
        //判断验证码是否正确
        String realCode = redisTemplate.opsForValue().get(mobile);
        //因为上面已经为传递过来的验证码判空，而redis中的验证码可能会超时所以将传过来的codee放在前面
        //防止异常
        if (!code.equals(realCode)) {
            return R.error().message("验证码超时或者不存在");
        }
        //判断手机号码是否存在
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        int count = ucenterMemberService.count(queryWrapper);
        System.out.println(count);
        if (count > 0) {
            throw new GuliException(20001,"该手机已经被注册了");
        }
        UcenterMember ucenterMember = new UcenterMember();
        //给密码加密
        password = MD5.encrypt(password);
        //将新建的值传给实体列
        BeanUtils.copyProperties(registerVo, ucenterMember);
        ucenterMember.setPassword(password);
        //设置默认图片
        ucenterMember.setAvatar("https://img1.baidu.com/it/u=996043041,139601738&fm=26&fmt=auto");
        //将改手机的信息传入
        ucenterMemberService.save(ucenterMember);
        return R.success();
    }



}

